package com.sgb.kjwl.view.ui.activity.self.publish;

import android.content.Context;
import android.content.Intent;
import androidx.databinding.DataBindingUtil;
import android.os.Bundle;
import android.view.View;

import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityPackersPublishNextBinding;
import com.sgb.kjwl.di.self.DaggerSelfComponent;
import com.sgb.kjwl.di.self.SelfModule;
import com.sgb.kjwl.model.entity.SuccessEntity;
import com.sgb.kjwl.model.entity.user.AddPackersEntity;
import com.swgk.core.event.EventConstant;
import com.sgb.kjwl.view.event.self.PackersPublishEvent;
import com.sgb.kjwl.view.ui.activity.self.authentication.CommitSuccessActivity;
import com.sgb.kjwl.viewmodel.self.publish.PackersNextModel;
import com.swgk.core.base.di.AppComponent;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.Objects;

import javax.inject.Inject;

import static com.sgb.kjwl.view.event.SuccessType.SUCC_PACK;

public class PackersPublishNextActivity extends AppActivity {
    @Inject
    PackersNextModel viewModel;

    public static void start(Context context) {
        start(context, null);
    }

    public static void start(Context context, Intent extras) {
        Intent intent = new Intent();
        intent.setClass(context, PackersPublishNextActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        if (extras != null) {
            intent.putExtras(extras);
        }
        context.startActivity(intent);
    }

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerSelfComponent.builder()
                .appComponent(appComponent)
                .selfModule(new SelfModule(this))
                .build()
                .inject(this);
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
    }

    @Override
    protected void initView() {
        ActivityPackersPublishNextBinding binding = DataBindingUtil.setContentView(PackersPublishNextActivity.this, R.layout.activity_packers_publish_next);
        binding.setViewModel(viewModel);
        binding.backView.setOnClickListener(this);
        binding.nextView.setOnClickListener(this);
    }

    @Override
    protected void init() {

    }

    public void showSuccess() {
//        MToast.showToast(PackersPublishNextActivity.this, "发布成功!");
        SuccessEntity successEntity = new SuccessEntity();
        successEntity.setTitle("发布成功");
        successEntity.setContent("您发布信息已提交成功，请前往我的工队页面查看");
        successEntity.setButtonText("确定");
        successEntity.setType(SUCC_PACK);
        CommitSuccessActivity.start(this, successEntity);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.back_view:
                finish();
                break;
            case R.id.next_view:
                if (Objects.requireNonNull(viewModel.addPackersEntity.get()).getId() != null) {
                    viewModel.editPackersModel();
                } else {
                    viewModel.addPackersModel();
                }
                break;
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    public void onEvent(PackersPublishEvent event) {
        if (event.getMessageTag() == EventConstant.EVENT_PUBLISH_PACKERS) {
            AddPackersEntity addPackersEntity = (AddPackersEntity) event.getMessageData();
            viewModel.addPackersEntity.set(addPackersEntity);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}

